import javax.swing.JFrame;
import java.awt.Dimension;
import java.util.Random;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.text.DecimalFormat;

import TJavaLib.TList;

public class wsnnetgen {
	private static Random random;
	private static DecimalFormat dF = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(Locale.US));
	private static TList net = new TList();
	
	
	/*--------------------------------------------------------------------------------------------*/
	//Syntax: wsngen / wsngen random 
	/*--------------------------------------------------------------------------------------------*/
	public static void main(String args[]) throws IOException {
   	if (args.length == 0) {
		  TFrame mainFrame = new TFrame();  
		  mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  mainFrame.setSize(new Dimension(1020, 680));
		  mainFrame.setVisible(true);
		} else if ("random".equalsIgnoreCase(args[0])) { 
			if (args.length < 7) {
				System.out.println("Syntax error!, wsngen takes 7 input parameters for a random scenario!");
				System.out.println("Example: wsngen random <number of nodes> <number of destinations> <width of dep. area> <height of dep. area> <transmission range> <sensing range>");
			} else {
				random = new Random();
				int nnode = Integer.parseInt(args[1]);
				int ndes = Integer.parseInt(args[2]);
				int depW = Integer.parseInt(args[3]);
				int depH = Integer.parseInt(args[4]);
				double rtx = Double.parseDouble(args[5]);
				double rs = Double.parseDouble(args[6]);
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("random.sce")));
				writer.write("Deployment Area;"+depW+";"+depH+"\n");
				writer.write("Node quantity;"+nnode+"\n");
				writer.write("Transmission range;"+dF.format(rtx).toString()+"\n");
				writer.write("Sensing range;"+dF.format(rs).toString()+"\n");
				int x = random.nextInt(depW);
				int y = random.nextInt(depH);
				writer.write("Gateway;"+x+";"+y+"\n");
			
				for (int i=0; i<nnode; i++) {
					TSensor s = new TSensor();
					x = random.nextInt(depW);
					y = random.nextInt(depH);
					s.setLoc(x, y);
					net.enList(s);
				}
			
				TList desGroup = new TList();
				for (int dc=0; dc<ndes; dc++) {
					boolean isin = true;
					int des = 0;
					while (isin) {
						des = random.nextInt(nnode);
						int j;for (j=0; j<desGroup.len(); j++) {
							int cp = ((Integer)desGroup.get(j)).intValue();
							if (cp==des) break;
						}
						if (j==desGroup.len()) isin = false;
					}
					desGroup.enList(Integer.valueOf(des));
					TSensor s = (TSensor)net.get(des);
					s.setType(TSensor.DESTINATION);
				}
			
				for (int i=0; i<nnode; i++) {
					TSensor s = (TSensor)net.get(i);
					writer.write("Sensor;"+s.getLoc().x+";"+s.getLoc().y);
					if (s.getType()==TSensor.DESTINATION)	
						writer.write(";des");
					writer.write("\n");
				}
				writer.close();
			}
		}
  }
}
